package com.ywl.study.transaction.intercept;

import com.ywl.study.transaction.transaction.DistirctTransactionManager;
import com.ywl.study.transaction.transaction.DistrictTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 自定义拦截器
 * 自定义拦截器后，需要配置进Spring
 *
 * 拦截器Interceptor可以拿到原始的HTTP请求和响应的信息，
 *    也可以拿到你真正处理请求方法的信息，但是拿不到传进参数的那个值。
 *
 *拦截顺序：filter—>Interceptor-->ControllerAdvice-->@Aspect -->Controller
 */


@Slf4j
@Component
public class RequestIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String groupId=request.getHeader("groupId");
        if(groupId!=null){
            log.info("从header中获得groupId");
            DistirctTransactionManager.setCurrentGroupId(groupId);
        }
        return true;
    }

}
